<?php

class GroupController extends MyController
{
	function init() { parent::init(); }
	
	function actionIndex()
	{
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website["beperpage"];
		
		$rows = group::model()->findAll(array(
			"order"=>"backend DESC, `order` ASC",
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		$pages->itemCount = group::model()->count();
		
		$this->data["rows"] = $rows;
		$this->data["page"] = $page;
		$this->data["pages"] = $pages;
		$this->render("view", $this->data);
	}
	
	function actionAdd($page = 1)
	{
		$this->action = "Thêm mới";
		$row = new group;
		$row->id = 0;
		$this->data["row"] = $row;
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($group_id = 0, $page = 1)
	{
		$this->action = "Cập nhật";
		if ($group_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view'));	
		}else{
			$row = group::model()->findByPk($group_id);
			$this->data["row"] = $row;
			$this->data["page"] = $page;
			$this->render("form_edit", $this->data);
		}
	}
	
	function actionSave($group_id = 0, $page = 1)
	{
		$group = NULL;
		if ($group_id == 0)
		{
			$row = new group;
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			
			$row = group::model()->findByPk($group_id);
			$row->setAttributes($_POST, false);
			
			if ($row->parent_id != $row->id && strlen($row->name) > 0)
				$row->save();
		}
		$this->redirect(array($this->controller_id.'/view', "page"=> $page));	
	}
	
	function actionApply($group_id = 0, $page = 1)
	{
		$group = NULL;
		if ($group_id == 0)
		{
			$row = new group;
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			
			$row = group::model()->findByPk($group_id);
			$row->setAttributes($_POST, false);
			
			if ($row->parent_id != $row->id && strlen($row->name) > 0)
				$row->save();
		}
		$this->redirect(array(
			$this->controller_id.'/edit', 
			"group_id" => $row->id, 
			"page"=> $page
		));	
	}
	
	function actionSavenew($group_id = 0, $page = 1)
	{
		$group = NULL;
		if ($group_id == 0)
		{
			$row = new group;
			$row->setAttributes($_POST, false);
			if (strlen($row->name) > 0)
				$row->save();
		}else{
			
			$row = group::model()->findByPk($group_id);
			$row->setAttributes($_POST, false);
			
			if ($row->parent_id != $row->id && strlen($row->name) > 0)
				$row->save();
		}
		$this->redirect(array(
			$this->controller_id.'/add',
			"page"=> $page
		));	
	}
	
	function actionEnable($group_id = 0,  $page = 1)
	{
		$row = group::model()->findByPk($group_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		$this->redirect(array($this->controller_id.'/view', "page"=> $page));
	}
	
	function actionDelete($group_id, $page = 1)
	{
		// chi xoa nhom rong.
		$group = group::model()->findByPk($group_id);
		$user_array = $group->users;
		
		$delete = 1;
		if (count($user_array) > 0)
			$delete = 0;
			
		if ($delete == 1)
		{
			grant::model()->deleteAll(array("condition"=>"group_id = " . $group_id));
			group::model()->deleteByPk($group_id);
		}
		
		$rows = group::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', "page" => $page));	
	}
	
	function actionSave_order($page = 1)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = group::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		$this->redirect(array($this->controller_id.'/view', "page"=> $page));	
	}
	
	
	function actionView_grant($current_group_id = 0)
	{
		$current_group = group::model()->findByPk($current_group_id);
		$this->data["current_group"] = $current_group;
		
		$this->render("view_grant", $this->data);
	}
	
	function actionRemove_grant($current_group_id = 0, $adminmenu_id = 0)
	{
		$adminmenu = adminmenu::model()->findByPk($adminmenu_id);
		
		if ($adminmenu->parent_id != 0)
		{
			grant::model()->deleteAll(array(
				"condition" => "group_id = ".$current_group_id . " AND adminmenu_id = ". $adminmenu->id
			));
		}
		else
		{
			// Tim va xoa phan quyen cac adminmenu con
			$adminmenu_children = adminmenu::model()->findAll(array(
				"condition" => "parent_id = " . $adminmenu_id
			));
			
			if (! is_null($adminmenu_children))
			{
				foreach($adminmenu_children as $row_child)
				{

					$grant = grant::model()->find(array(
						"condition" => "group_id = ".$current_group_id . " AND adminmenu_id = ". $row_child->id
					));
					
					if (! is_null($grant))
					{
						grant::model()->deleteAll(array(
							"condition" => "group_id = ".$current_group_id . " AND adminmenu_id = ". $row_child->id
						));
					}
				}
			}
			
			// Xoa quyen adminmenu
			grant::model()->deleteAll(array(
				"condition" => "group_id = " . $current_group_id . " AND adminmenu_id = " . $adminmenu_id
			));
		}
		
		$this->redirect(array($this->controller_id."/view_grant", "current_group_id" => $current_group_id));
	}
	
	function actionView_adminmenu($current_group_id = 0)
	{
		$adminmenu_parent = adminmenu::model()->findAll(array(
			"condition" => "parent_id = 0"
		));
		$this->data["adminmenu_parent"] = $adminmenu_parent;
		$this->data["current_group_id"] = $current_group_id;
		
		$this->render("view_adminmenu", $this->data);
	}
	
	function actionAdd_grant($current_group_id = 0 , $adminmenu_id = 0)
	{
		$grant = grant::model()->find(array(
			"condition" => "group_id = " .$current_group_id . " AND adminmenu_id = " . $adminmenu_id
		));
		
		// Neu chua co thi them
		if (is_null($grant))
		{
			$adminmenu = adminmenu::model()->findByPk($adminmenu_id);
			if ($adminmenu->parent_id == 0)
			{
				$new_grant = new grant;
				$new_grant->group_id = $current_group_id;
				$new_grant->adminmenu_id = $adminmenu_id;
				$new_grant->save();
			}
			else
			{
				$count_grant = grant::model()->count(array(
					"condition" => "group_id = " .$current_group_id . " AND adminmenu_id = " . $adminmenu->parent_id
				));
				
				if ($count_grant == 0)
				{
					$new_grant_parent = new grant;
					$new_grant_parent->group_id = $current_group_id;
					$new_grant_parent->adminmenu_id = $adminmenu->parent_id;
					$new_grant_parent->save();
				}
				
				$new_grant = new grant;
				$new_grant->group_id = $current_group_id;
				$new_grant->adminmenu_id = $adminmenu_id;
				$new_grant->save();
			}
		}
		
		$this->redirect(array($this->controller_id."/view_grant", "current_group_id" => $current_group_id));
	}
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				// chi xoa nhom rong.
				$group = group::model()->findByPk($id);
				$user_array = $group->users;
				
				$delete = 1;
				if (count($user_array) > 0)
					$delete = 0;
					
				if ($delete == 1)
				{
					grant::model()->deleteAll(array("condition"=>"group_id = " . $id));
					group::model()->deleteByPk($id);
				}
			}
		}
		
		$rows = group::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', "page"=> $page));
	}
	
}